using DimensionsHelper.LanguageService.Symbols;
using DimensionsHelper.LanguageService.Syntax;

namespace DimensionsHelper.LanguageService;

public class AnalysisMemberExpression : AnalysisExpression
{
    internal AnalysisMemberExpression(MemberExpression syntaxNode,
        AnalysisExpression? obj,
        AnalysisIdentifier member,
        AnalysisNode? parent) :
        base(AnalysisBoundKinds.Member, syntaxNode, parent)
    {
        Syntax = syntaxNode;

        Object = obj;

        if (Object != null)
        {
            Object.Parent = this;
        }

        Member = member;
        Member.Parent = this;
    }


    public AnalysisExpression? Object { get; }

    public AnalysisIdentifier Member { get; }

    public override MemberExpression Syntax { get; }


    public override int ChildrenCount => Object == null ? 1 : 2;


    public override IEnumerable<AnalysisNode> GetChildren()
    {
        if (Object == null)
        {
            return [Member];
        }

        return [Object, Member];
    }

    internal override void AddChild(AnalysisNode child)
    {
        throw new NotSupportedException("AnalysisMemberExpression is unchangeable.");
    }
}

public static partial class AnalysisNodeFactory
{
    /// <summary>
    /// 创建一个成员调用表达式
    /// </summary>
    /// <param name="syntaxNode"></param>
    /// <param name="obj">对象表达式</param>
    /// <param name="member">成员表达式</param>
    /// <param name="parent">父节点，可以忽略</param>
    /// <param name="withSymbol">语法节点所在的With语句上下文类型符号</param>
    public static AnalysisMemberExpression MemberExpression(MemberExpression syntaxNode, AnalysisExpression? obj,
        AnalysisIdentifier member, AnalysisNode? parent, ITypeSymbol? withSymbol)
    {
        return new AnalysisMemberExpression(syntaxNode, obj, member, parent).WithContainingWithSymbol(withSymbol);
    }
}